<?php
/**
 *开宝箱
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\ActivityCouponMod;
use Model\ActivityMemberMod;
use Model\ActivityMemberTreasureMod;
use Model\ActivityMod;
use Model\ActivityTreasureMod;
use Model\GiveCouponMod;
use Model\SponsorCouponMod;
use Pri\snapshot\CouponSnapshot;
use Pub\cache\Cache;
use Pub\date\Date;
use Pub\iarray\IArray;

class OpenBz extends Action
{
    private $_activity_id = 0;  //活动id
    private $_uid = 0;  //用户id
    private $_activity = array();  //活动信息
    private $_open_bz_types = array();  //宝藏类型的数组
    private $_bz = array(); //虚拟宝藏的列表
    private $_entity = array();  //实体奖品的列表
    private $_coupon = array();  //卡券列表
    private $_return_bz = array();  //抽中的宝藏，返回的宝藏数据

    private $ActivityMember = null;  //活动的会员表
    private $ActivityMemberTreasure = null;  //会员开到的宝藏
    private $ActivityTreasure = null;  //活动投放的宝藏
    private $ActivityCoupon = null; //活动投放的卡券
    private $SponsorCoupon = null;  //举办方投放的卡券
    private $GiveCoupon = null;  //门店发券记录

	public function __construct()
	{
		parent::__construct();
		if(isset($this->_gpc['activity_id'])) $this->_activity_id = $this->_gpc['activity_id'];
		if(isset($this->_gpc['uid'])) $this->_uid = $this->_gpc['uid'];
		if($this->dataSignCheck() != $this->_gpc['sign']){
            $this->_result = array('status'=>'error','err_status'=>-2,'err_msg'=>'宝箱开启失败，请重试');
            $this->result($this->_result);
        }
	}

    /**
     * 开宝箱数据验证
     */
	private function dataSignCheck()
    {
        $_post_data = $_POST;
        unset($_post_data['sign']);
        $_key_arr = array_keys($_post_data);
        sort($_key_arr,SORT_STRING);
        $_key_str = '';
        foreach($_key_arr as $_v){
            $_key_str .= $_post_data[$_v];
        }
        return strtoupper(md5($_key_str));
    }

    /**
     * 开宝箱
     * @param activity_id
     */
	public function index()
    {
        $this->_activity = Cache::get(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_activity_id));
        if(!$this->_activity){
            $this->_activity = $this->getActivity(new ActivityMod(),$this->_activity_id);
        }
        //step1 验证活动进行状态
        $this->checkTimeStatus();

        //step2 如果活动不限制用户打开数量则验证当前用户打开宝藏的数量
        if($this->_activity['init_bz_number'] > 0){
            $this->checkUserOpenNumber();
        }

        //step3 生成一个活动投放宝藏类型的数组
        $this->_open_bz_types = $this->getOpenBzType();

        //step4 通过宝藏投放类型数组返回一个宝藏类型
                //1、循环查询各个宝藏类型，是否还有宝藏，如果有宝藏则返回宝藏的数据列表
        foreach($this->_open_bz_types as $_k=>$_v){
            if($_treasure = $this->getBzTypeNumber($_v['type'])){
                if($_v['type'] == 1){
                    $this->_bz = $_treasure;
                }else if($_v['type'] == 2){
                    $this->_entity = $_treasure;
                }else{
                    $this->_coupon = $_treasure;
                }
            }else{
                unset($this->_open_bz_types[$_k]);
            }
        }


        //step5  检测宝藏类型中的长度是否为0，如果是0代表活动的宝藏已经投放完成
        if(count($this->_open_bz_types) <= 0){
            $this->_result = array('status'=>'error','err_status'=>-3,'err_msg'=>'您暂无可开奖品了哦~~');
            $this->result($this->_result);
        }

        //step6 返回一个抽中的奖品类型
        $_return_type = $this->getReturnType();

        //step7 随机取出抽中类型的一个宝藏，最后返回
        $this->_return_bz = $this->takeReturnBz($_return_type);
        if(!$this->_return_bz){
          $this->addMemberTreasure(true);
          $this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'运气不佳（╯＿╰）这是一个空宝箱');
          $this->result($this->_result);
        }


        //step8 新增会员的开宝藏记录
        $this->addMemberTreasure();

        //step9 新增开到的宝藏give_number记录
        $this->addBzGiveNumber();

        //step10 新增门店卡券发放记录
        if($this->_return_bz['type'] == 3){
            $this->storePutCouponRecord(); //新增门店卡券发放记录
        }

        //step 最后的宝藏数据处理
            //1、宝藏图片处理
        if(isset($this->_return_bz['icon'])) $this->_return_bz['icon'] = tomedia($this->_return_bz['icon']);
            //2、门店logo处理
        if(isset($this->_return_bz['logo'])){
            $this->_return_bz['logo'] = tomedia($this->_return_bz['logo']);
        }
            //3、卡券封面
        if(isset($this->_return_bz['cover'])){
            $this->_return_bz['cover'] = tomedia($this->_return_bz['cover']);
        }
            //4、删除一些无用数据
            unset($this->_return_bz['images'],$this->_return_bz['id'],$this->_return_bz['win_odds'],$this->_return_bz['user_number'],$this->_return_bz['rule'],$this->_return_bz['coupon_id'],$this->_return_bz['edit_time']);
        $this->_result = array('status'=>'success','data'=>$this->_return_bz);
        $this->result($this->_result);
    }

    /**
     * 验证活动状态
     */
    private function checkTimeStatus()
    {
        $_time_status = Date::time2time($this->_activity['start'],$this->_activity['end']);
        if($_time_status != 2){
            $this->_result = array(
                'status'=>'error',
                'err_msg'=>$_time_status == 1 ? '活动未开始' : '活动已结束',
                'err_status'=>-1
            );
            $this->result($this->_result);
        }
    }

    /**
     * 验证用户当前打开的宝藏数量
     */
    private function checkUserOpenNumber()
    {
        if(!$this->ActivityMember instanceof ActivityMemberMod) $this->ActivityMember = new ActivityMemberMod();
        if(!$this->ActivityMemberTreasure instanceof ActivityMemberTreasureMod) $this->ActivityMemberTreasure = new ActivityMemberTreasureMod();
        $_activity_member = $this->ActivityMember->findOne(array('activity_id'=>$this->_activity_id,'uid'=>$this->_uid),array('open_num'));
        $_can_open_number = $_activity_member['open_num'] + $this->_activity['init_bz_number'];
        $_already_open_number = $this->ActivityMemberTreasure->sum(array('activity_id'=>$this->_activity_id,'uid'=>$this->_uid,'source'=>1),'number');
        if($_already_open_number >= $_can_open_number){
            $this->_result = array('status'=>'error','err_msg'=>'可开宝箱数量不足','err_status'=>-2);
            $this->result($this->_result);
        }
    }

    /**
     * 获取打开返回宝藏的类型
     */
    private function getOpenBzType()
    {
        $_types = array();
        if($this->_activity['put_bz']) $_types['bz'] = array('type'=>1,'win_odds'=>$this->_activity['bz_win_odds']);
        if($this->_activity['put_entity']) $_types['entity'] = array('type'=>2,'win_odds'=>$this->_activity['entity_win_odds']);
        if($this->_activity['put_coupon'] || $this->_activity['sponsor_coupon']){
            //卡券来自哪里
            $_from = '';  //store=门店 sponsor=举办方
            //如果门店卡券和举办方卡券同时投放，则随机数1=门店卡券，2=举办方卡券
            if($this->_activity['put_coupon'] && $this->_activity['sponsor_coupon']){
                $_from = mt_rand(1,2) == 1 ? 'store' : 'sponsor';
            }else if($this->_activity['put_coupon']){
                $_from = 'store';
            }else{
                $_from = 'sponsor';
            }
            $_types['coupon'] = array('type'=>3,'win_odds'=>$this->_activity['coupon_win_odds'],'from'=>$_from);
        }
        return $_types;
    }

    /**
     * 获取指定宝藏类型的数量是否足够，足够返回列表
     * @param $_type
     * @return boolean 返回指定类型的宝藏是否足够
     */
    private function getBzTypeNumber($_type)
    {
        switch($_type){
            case 1:
                return $this->getBzNumber($_type);
            case 2:
                return $this->getBzNumber($_type);
            case 3:
                return $this->returnCouponNumber();
        }
    }

    /**
     * 获取宝藏的数量是否足够，足够返回列表
     * @param type 宝藏类型
     * @return boolean 宝藏数量是否足够
     */
    private function getBzNumber($_type)
    {
        if(!$this->ActivityTreasure instanceof ActivityTreasureMod) $this->ActivityTreasure = new ActivityTreasureMod();
        $_bz_list = $this->ActivityTreasure->getBz($this->_activity_id,$_type);
        $_return_list = array();
        if($_bz_list && count($_bz_list)>0){
            foreach($_bz_list as $_k=>$_v){
                //去除当前用户已经获得足够的宝藏
                if(($_v['user_number'] > 0) && ($this->getUserBzNumber($_v['id']) >= $_v['user_number'])){
                    continue;
                }
                $_return_list[] = $_v;
            }
        }else{
            return false;
        }
        return count($_return_list) > 0 ? $_return_list : false;
    }

    /**
     * 返回卡券列表，返回门店卡券或者举办方投放的卡券
     */
    private function returnCouponNumber()
    {
        if($this->_open_bz_types['coupon']['from'] == 'store'){
            //返回门店卡券列表
            return $this->getCouponNumber();
        }else{
            //返回举办方投放的卡券列表，如果是空列表，判断是否有门店卡券
            //如果有怎更改from,则递归执行
            $_coupon_list = $this->getSponsorCoupon();
            if(!$_coupon_list){
                if($this->_activity['put_coupon']){
                    $this->_open_bz_types['coupon']['from'] = 'store';
                    return $this->returnCouponNumber();
                }else{
                    return false;
                }
            }else{
                return $_coupon_list;
            }
        }
    }

    /**
     * 获取卡券的数量是否足够
     * @return boolean 卡券是否足够
     */
    private function getCouponNumber()
    {
        //列表条件 1、卡券门店状态正常
                //2、卡券未过期
                //3、卡券有库存
                //4、当前用户是否已经抢到足够的卡券
        if(!$this->ActivityCoupon instanceof ActivityCouponMod) $this->ActivityCoupon = new ActivityCouponMod();
        $_coupon_limit = array(0,10);
        $_coupon_total = $this->ActivityCoupon->putCouponDataTotal($this->_activity_id);
        if($_coupon_total > 10){
            $_limit_random = mt_rand(0,$_coupon_total-1);
            $_coupon_limit[0] = $_limit_random;
        }
        $_coupon_list = $this->ActivityCoupon->putCoupon($this->_activity_id,$_coupon_limit);
        if($_coupon_list && count($_coupon_list) > 0){
            $_return_list = array();
            foreach($_coupon_list as $_k=>$_v){
                //去除当前用户已经获得足够的宝藏
                if($_v['user_number'] > 0 && $this->getUserBzNumber($_v['id'],'coupon') >= $_v['user_number']){
                    array_splice($_coupon_list,$_k,1);
                    continue;
                }
                //去除超时的卡券
                if($_v['use_limit_type'] == 2 && ($_v['limit_time']+86400) < TIMESTAMP){
                    array_splice($_coupon_list,$_k,1);
                    continue;
                }
                //获取卡券的面值信息
                $_v['coupon_type'] = $_v['type'];
                $_face = $this->costToStrcost($_v['type'],$_v['cost']);
                $_v['cost_type'] = $_face['type'];
                $_v['cost'] = $_face['face_value'];
                unset($_v['user_number'],$_v['type']);
                //卡券的时间限制
                $_v['period'] = $this->limitTimeTostr($_v['use_limit_type'],$_v['limit_time']);
                $_v['type'] = 3;
                $_return_list[] = $_v;
            }
        }else{
            return false;
        }
        return $_return_list;
    }

    /**
     * 获取举办方投放的卡券
     * @return array|bool
     */
    private function getSponsorCoupon()
    {
        if(!$this->SponsorCoupon instanceof SponsorCouponMod) $this->SponsorCoupon = new SponsorCouponMod();
        $_coupon_list = $this->SponsorCoupon->getPutList($this->_activity_id);
        if($_coupon_list && count($_coupon_list) > 0){
            $_return_list = array();
            foreach($_coupon_list as $_k=>$_v){
                //去除当前用户已经获得足够的宝藏
                if($_v['user_number'] > 0 && $this->getUserBzNumber($_v['id'],'sponsor_coupon') >= $_v['user_number']){
                    continue;
                }
                //去除超时的卡券
                if($_v['use_limit_type'] == 2 && ($_v['limit_time']+86400) < TIMESTAMP){
                    continue;
                }
                //卡券的时间限制
                $_v['period'] = $this->limitTimeTostr($_v['use_limit_type'],$_v['limit_time']);
                $_v['type'] = 4;
                $_return_list[] = $_v;
            }
            unset($_coupon_list);
            return $_return_list;
        }else{
            return false;
        }
    }

    /**
     *通过卡券的时间限制类型use_limit_type和时间限制value(limit_time)返回字符串
     * @param  $_type 限制类型
     * @param $_value 值
     * @return 卡券时间限制的字符串
     */
    private function limitTimeTostr($_type,$_value)
    {
        $_content = '';
        if($_type == '1'){
            $_content = '永久有效';
        }else if($_type == '2'){
            $_content = '有效期至'.date('Y-m-d',$_value);
        }else{
            $_content = '有效期至'.date('Y-m-d',TIMESTAMP+86400*$_value);
        }
        return $_content;
    }

    /**
     * 用户抢到的指定宝藏数量
     * @param $_treasure_id 宝藏id
     * @param $_type 宝藏类型  bz=宝藏(虚拟宝藏和实体奖励)  coupon=卡券
     * @return number 返回用户抢到的宝藏数量
     */
    private function getUserBzNumber($_treasure_id,$_type = 'bz')
    {
        $_number = 0;
        $_where = array('activity_id'=>$this->_activity_id,'uid'=>$this->_uid,'treasure_id'=>$_treasure_id);
        if($_type == 'bz'){
            $_where['type <'] = 3;
        }else if($_type == 'coupon'){
            $_where['type'] = 3;
        }else if($_type == 'sponsor_coupon'){
            $_where['type'] = 4;
        }
        if(!$this->ActivityMemberTreasure instanceof ActivityMemberTreasureMod) $this->ActivityMemberTreasure = new ActivityMemberTreasureMod();
        if($this->ActivityMemberTreasure->findOne($_where,array('id'))){
            $_number = $this->ActivityMemberTreasure->sum($_where,'number');
        }
        return $_number;
    }

    /**
     * 获取宝藏类型
     */
    private function getReturnType()
    {
        //1、吧各个类型的中奖率按照从小到大的顺序排列
        $_win_odds_arr = array();
        $_win_odds = 0;
        foreach($this->_open_bz_types as $_k=>$_v){
            $_data = array('win_odds'=>$_v['win_odds'],'type'=>$_v['type']);
            if($_v['win_odds'] >= $_win_odds){
                $_win_odds = $_v['win_odds'];
                array_push($_win_odds_arr,$_data);
            }else{
                array_unshift($_win_odds_arr,$_data);
            }
        }

        //2生成一个数组，各个宝藏分类的范围
        $_limit_one = 1;
        $_prev_win_odds = 0;
        foreach($_win_odds_arr as $_k=>$_v){
            $_win_odds_arr[$_k]['limit'] = array($_limit_one,$_v['win_odds']+$_prev_win_odds);
            $_limit_one += $_v['win_odds'];
            $_prev_win_odds += $_v['win_odds'];
        }

        //3、生成一个随机数，1到各个宝藏中奖率最大的随机数
        $_rand_number = mt_rand(1,$_win_odds_arr[count($_win_odds_arr)-1]['limit'][1]);

        //4、循环对比，如果随机数在某个宝藏类型的limit内则返回该宝藏的类型
        foreach($_win_odds_arr as $_v){
            $_limit_min = $_v['limit'][0];
            $_limit_max = $_v['limit'][1];
            if($_rand_number >= $_v['$_limit_min'] && $_rand_number <=$_limit_max){
                return $_v['type'];
                break;
            }
        }
    }

    /**
     * 取出一个返回的宝藏
     * @param 要返回的宝藏类型
     * @return boolean false 空宝箱 || data 返回开到的宝藏
     */
    private function takeReturnBz($_type)
    {
        $_return_bz = false;
        if($_type == 1){
            //返回一个宝藏,从宝藏列表的中奖率较大的开始循环，在循环过程中算出要取出的宝藏，随机出1到中奖率大小的随机数，如果随机数等于1则返回该宝藏！！
            if($this->sameWinOddsGreaterTwo($this->_bz)){
                $this->_bz = $this->sameWinOddsRandom($this->_bz);
            }
            foreach($this->_bz as $_v){
                $_rand = mt_rand(1,$_v['win_odds']);
                if($_rand == 1){
                    $_return_bz = $_v;
                    break;
                }
            }
        }else if($_type == 2){
            //返回一个实物奖励,通过随机1到每个宝藏的中奖率，如果等于1则返回该奖励，如果随机到任何奖励，则再次请求该方法
            if($this->sameWinOddsGreaterTwo($this->_entity)){
                $this->_entity = $this->sameWinOddsRandom($this->_entity);
            }
            foreach($this->_entity as $_k=>$_v){
                $_rand = mt_rand(1,$_v['win_odds']);
                if($_rand == 1){
                    $_return_bz = $_v;
                    break;
                };
            }
        }else{
            //返回一个卡券
            //、生成一个随机下标，返回随机下标的值
            if(count($this->_coupon) > 0){
                if($this->_open_bz_types['coupon']['from'] == 'store'){
                    $_rand_index = mt_rand(0,count($this->_coupon)-1);
                    $_return_bz = $this->_coupon[$_rand_index] ? $this->_coupon[$_rand_index] : false;
                }else{
                    if($this->sameWinOddsGreaterTwo($this->_coupon)){
                        $this->_coupon = $this->sameWinOddsRandom($this->_coupon);
                    }
                    foreach($this->_coupon as $_k=>$_v){
                        $_rand = mt_rand(1,$_v['win_odds']);
                        if($_rand == 1){
                            $_return_bz = $_v;
                            break;
                        };
                    }
                }
            }
        }
        if(count($this->_open_bz_types) > 0 && !$_return_bz){
            $this->_open_bz_types = IArray::shift($this->_open_bz_types,$_type,'type');
            return $this->takeReturnBz($this->_open_bz_types[0]['type']);
        }
        return $_return_bz;
    }

    /**
     * 一组奖品是否有多个中奖率是1的奖品
     * @param $_award 奖品数组
     * @return boolean
     */
    private function sameWinOddsGreaterTwo(Array $_award)
    {
        $_win_ono_count = 0;
        foreach($_award as $_v){
            if(!isset($_v['win_odds'])) return false;
            if($_v['win_odds'] == 1) $_win_ono_count++;
            if($_win_ono_count > 1) return true;
        }
        return false;
    }

    /**
     * 如果一组奖品有N个相同的中奖率等于1,则打乱顺序随机返回一个中奖率是1的宝藏
     * @param $_award 奖品数组
     * @return boolean
     */
    private function sameWinOddsRandom(Array $_award)
    {
        $_won_odds = array();
        foreach($_award as $_k=>$_v){
            if($_v['win_odds'] == 1){
                $_won_odds[] = $_v;
                unset($_award[$_k]);
            }
        }
        shuffle($_won_odds);
        return array_merge($_award,$_won_odds);
    }

    /**
     * 新增用户开宝箱的记录
     * @param $_empty 是不是空的宝箱
     * @return boolean 是否新增成功
     */
    private function addMemberTreasure($_empty = false)
    {
        if($_empty){
            $_type = $_treasure_id = 0;
        }else{
            $_treasure_id = $this->_return_bz['id'];
            $_type = $this->_return_bz['type'];
        }
        $_data = array(
            'uid'=>$this->_gpc['uid'],
            'activity_id'=>$this->_gpc['activity_id'],
            'treasure_id'=>$_treasure_id,
            'type'=>$_type
        );
        if(!$this->ActivityMemberTreasure instanceof ActivityMemberTreasureMod) $this->ActivityMemberTreasure = new ActivityMemberTreasureMod();
        $_one_entity = $this->ActivityMemberTreasure->findOne($_data,array('id'));
       if($this->_return_bz['type'] == 3){
           $this->addCouponSnapshot();
       }
       if($this->_return_bz['type'] == 4) $this->addSponsorCouponSnapshot();
        if($_one_entity){
            return $this->ActivityMemberTreasure->math(array('number +'=>1),array('id'=>$_one_entity['id']));
        }else{
            $_data['type'] = $_type;
            return $this->ActivityMemberTreasure->insert($_data);
        }
    }

    /**
     * 新增开到的宝藏开启数量give_number
     */
    private function addBzGiveNumber()
    {
        if($this->_return_bz['type'] == 1 || $this->_return_bz['type'] == 2){
            if(!$this->ActivityTreasure instanceof ActivityTreasureMod) $this->ActivityTreasure = new ActivityTreasureMod();
            $this->ActivityTreasure->math(array('give_number +'=>1),array('id'=>$this->_return_bz['id']));
        }else if($this->_return_bz['type'] == 3){
            if(!$this->ActivityCoupon instanceof ActivityCouponMod) $this->ActivityCoupon = new ActivityCouponMod();
            $this->ActivityCoupon->math(array('give_number +'=>1),array('id'=>$this->_return_bz['id']));
        }else if($this->_return_bz['type'] == 4){
            if(!$this->SponsorCoupon instanceof SponsorCouponMod) $this->SponsorCoupon = new SponsorCouponMod();
            $this->SponsorCoupon->math(array('give_number +'=>1),array('id'=>$this->_return_bz['id']));
        }
    }

    /**
     * 记录门店发券记录
     */
    private function storePutCouponRecord()
    {
        if(!$this->GiveCoupon instanceof GiveCouponMod) $this->GiveCoupon = new GiveCouponMod();
        $this->GiveCoupon->insert(array(
            'store_id'=>$this->_return_bz['store_id'],
            'time'=>TIMESTAMP
        ));
    }

    /**
     * 新增卡券快照
     */
    private function addCouponSnapshot()
    {
        $_data = array(
            'activity_id'=>$this->_activity_id,
            'store_id'=>$this->_return_bz['store_id'],
            'uid'=>$this->_uid,
            'coupon_id'=>$this->_return_bz['coupon_id'],
            'activity_coupon_id'=>$this->_return_bz['id'],
            'type'=>$this->_return_bz['coupon_type'],
            'name'=>$this->_return_bz['name'],
            'cost'=>$this->_return_bz['cost'],
            'rule'=>$this->_return_bz['rule'],
            'cost_type'=>$this->_return_bz['cost_type'],
            'past_time'=>$this->couponPastTime(),
            'edit_time'=>$this->_return_bz['edit_time']
        );
        if($this->storeCouponCover()){
            $_data['cover'] = $this->_return_bz['cover'];
            $_data['images'] = $this->_return_bz['images'];
        }
        $CouponSnapshot = new CouponSnapshot($_data);
        $CouponSnapshot->save();
    }

    /**
     * 获取门店的卡券封面
     */
    private function storeCouponCover()
    {
        //处理门店卡券的卡券封面和卡券的所有相关图片
        $_images = json_decode($this->_return_bz['images'],true);
        if(count($_images) > 0){
            $this->_return_bz['images'] = $_images;
            $this->_return_bz['cover'] = $_images[0];
            return true;
        }
        unset($_images,$this->_return_bz['images']);
        return false;
    }

    /**
     * 新增活动后台投放卡券的快照
     */
    private function addSponsorCouponSnapshot()
    {
        $CouponSnapshot = new CouponSnapshot(array(
            'activity_id'=>$this->_activity_id,
            'uid'=>$this->_uid,
            'coupon_id'=>$this->_return_bz['id'],
            'name'=>$this->_return_bz['name'],
            'icon'=>$this->_return_bz['icon'],
            'rule'=>$this->_return_bz['rule'],
            'money'=>$this->_return_bz['money'],
            'past_time'=>$this->couponPastTime(),
            'edit_time'=>$this->_return_bz['edit_time']
        ));
        $CouponSnapshot->save('snapshot');
    }

    /**
     * 返回卡券过期时间
     */
    private function couponPastTime()
    {
        $_past_time = 1;
        if($this->_return_bz['use_limit_type'] == 2){
            $_past_time = $this->_return_bz['limit_time'];
        }else if($this->_return_bz['use_limit_type'] == 3){
            $_past_time = strtotime(date('Y-m-d',TIMESTAMP)) + $this->_return_bz['limit_time'] * 86400;
        }
        return $_past_time;
    }


}